package com.snailxr.base.task.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.snailxr.base.support.RetObj;
import com.snailxr.base.task.domain.DataDictionary;
import com.snailxr.base.task.service.DataDicService;

@Controller
@RequestMapping("/ddic")
public class DataDicController {
	// 日志记录器
	public final Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private DataDicService dataDicService;
	@RequestMapping("ddicList")
	public String list(HttpServletRequest request){
		List<DataDictionary> ddics = dataDicService.getDicList();
		log.debug(ddics.toString());
		request.setAttribute("ddics", ddics);
		return "base/ddic/ddicList";
	}
	
	@RequestMapping("add")
	@ResponseBody
	public RetObj add(HttpServletRequest request,DataDictionary ddic){
		RetObj retObj = new RetObj();
		//TODO 还需要做数据验证
		try{
			dataDicService.addJxOrg2Chnl(ddic);
			retObj.setMsg("保存成功！");
		}catch(Exception e){
			retObj.setMsg("保存失败！");
		}
		return retObj;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public RetObj delete(HttpServletRequest request,int dicId){
		RetObj retObj = new RetObj();
		//TODO 还需要做数据验证
		try{
			DataDictionary ddic = dataDicService.getJxOrg2ChnlByDicId(dicId);
			dataDicService.deleteJxOrg2Chnl(ddic);
			retObj.setMsg("删除成功！");
		}catch(Exception e){
			retObj.setMsg("删除失败！");
		}
		return retObj;
	}
	
	@RequestMapping("update")
	@ResponseBody
	public RetObj update(HttpServletRequest request,DataDictionary ddic){
		RetObj retObj = new RetObj();
		//TODO 还需要做数据验证
		log.debug(ddic);
		dataDicService.updateJxOrg2Chnl(ddic);
		retObj.setMsg("更新成功！");
		return retObj;
	}
}
